class iterator {
    typename std::vector<std::list<KeyValue>>::iterator vecIt;
    typename std::list<KeyValue>::iterator listIt;
    std::vector<std::list<KeyValue>>* table;

public:
    iterator(std::vector<std::list<KeyValue>>* t,
        typename std::vector<std::list<KeyValue>>::iterator vi,
        typename std::list<KeyValue>::iterator li)
        : table(t), vecIt(vi), listIt(li) {}

    KeyValue& operator*() { return *listIt; }
    KeyValue* operator->() { return &(*listIt); }

    iterator& operator++() {
        ++listIt;
        if (listIt == vecIt->end()) {
            ++vecIt;
            while (vecIt != table->end() && vecIt->empty()) {
                ++vecIt;
            }
            if (vecIt != table->end()) {
                listIt = vecIt->begin();
            }
        }
        return *this;
    }

    bool operator!=(const iterator& other) const {
        return vecIt != other.vecIt || listIt != other.listIt;
    }
};

iterator begin() {
    auto vecIt = table.begin();
    while (vecIt != table.end() && vecIt->empty()) {
        ++vecIt;
    }
    if (vecIt != table.end()) {
        return iterator(&table, vecIt, vecIt->begin());
    }
    return end();
}

iterator end() {
    return iterator(&table, table.end(), typename std::list<KeyValue>::iterator());
}
